import 'package:flutter/material.dart';
import 'package:mobile_app.core/core/presentation/theme/styles.dart';

import 'text_theme.dart';

class LearningCoreTheme extends InheritedTheme {
  LearningCoreTheme({
    required Brightness brightness,
    required super.child,
    super.key,
  })  : _brightness = brightness,
        _data = _buildThemeData(
          brightness: brightness,
        );

  final Brightness _brightness;
  final LearningCoreThemeData _data;

  static LearningCoreThemeData _buildThemeData({
    required Brightness brightness,
  }) {
    return switch (brightness) {
      Brightness.dark => LearningCoreThemeData.dark(),
      Brightness.light => LearningCoreThemeData.light(),
    };
  }

  static LearningCoreThemeData of(BuildContext context) {
    final inheritedTheme = context.dependOnInheritedWidgetOfExactType<LearningCoreTheme>()!;
    return inheritedTheme._data;
  }

  @override
  Widget wrap(BuildContext context, Widget child) {
    final ancestorTheme = context.findAncestorWidgetOfExactType<LearningCoreTheme>();

    if (identical(this, ancestorTheme)) {
      return child;
    }

    return LearningCoreTheme(
      brightness: _brightness,
      child: child,
    );
  }

  @override
  bool updateShouldNotify(LearningCoreTheme oldWidget) {
    return _brightness != oldWidget._brightness;
  }
}

class LearningCoreThemeData {
  const LearningCoreThemeData._internal({
    required this.segmentedProgressBarBackgroundColor,
    required this.textTheme,
  });

  factory LearningCoreThemeData.light() {
    return LearningCoreThemeData._internal(
      segmentedProgressBarBackgroundColor: CoreStyles.lightGrey3,
      textTheme: LearningCoreTextTheme.light(),
    );
  }

  factory LearningCoreThemeData.dark() {
    return LearningCoreThemeData._internal(
      segmentedProgressBarBackgroundColor: CoreStyles.darkGrey1,
      textTheme: LearningCoreTextTheme.dark(),
    );
  }

  final Color segmentedProgressBarBackgroundColor;
  final LearningCoreTextTheme textTheme;
}
